package com.fintech.third.service.moxie;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fintech.common.enums.RespStatus;
import com.fintech.common.service.BaseServiceImpl;
import com.fintech.common.util.ApiResponse;
import com.fintech.third.constant.ThirdMethodConstants;
import com.fintech.third.model.MoxieTaskInfo;
import com.fintech.third.model.ThirdEffectDTO;
import com.fintech.third.model.moxie.MoXieTaskResult;
import com.fintech.third.model.moxie.MoXieTaskType;
import com.fintech.third.model.moxie.MoxieResponse;
import com.fintech.third.model.moxie.RealNameAuthentication;
import com.fintech.third.model.moxie.alipay.MXAlipayReport;
import com.fintech.third.model.moxie.alipay.MXAlipayZmsReport;
import com.fintech.third.model.moxie.carries.AllDataDetails;
import com.fintech.third.model.moxie.carries.BillDetail;
import com.fintech.third.model.moxie.carries.MXUserReportCall;
import com.fintech.third.model.moxie.carries.MobileBasicV3;
import com.fintech.third.model.moxie.carries.NumberReport;
import com.fintech.third.model.moxie.carries.ShortMessageDetailV2;
import com.fintech.third.model.moxie.carries.VoiceCallDetailList;
import com.fintech.third.model.moxie.carriesv2.MXReport;
import com.fintech.third.model.moxie.chsi.StudentInfoList;
import com.fintech.third.model.moxie.fund.BillRecord;
import com.fintech.third.model.moxie.fund.LoanInfo;
import com.fintech.third.model.moxie.fund.LoanRepayRecord;
import com.fintech.third.model.moxie.fund.UserFundRecord;
import com.fintech.third.model.moxie.fund.UserInfo;
import com.fintech.third.model.moxie.insurance.InsurancePolicy;
import com.fintech.third.model.moxie.mail.EmailBillInstallmentList;
import com.fintech.third.model.moxie.mail.EmailBillList;
import com.fintech.third.model.moxie.mail.EmailBillShoppingSheetList;

import net.sf.json.JSONObject;

@Service("freshMoXie")
public class FreshMoxieImpl extends BaseServiceImpl implements MoXieService {
	@Autowired(required = true)
	private MoXieService MoXie;

	private ApiResponse<?> mxCommon(Map<String, String> dataMap, String serialNum, String mobile, String loanId,
			String channel, String interfaceCode, ApiResponse<?> apiResponse, String taskId) {
		// 获取时效性相关数据
		ThirdEffectDTO effectDto = new ThirdEffectDTO();
		// 获取时效性相关数据
		effectDto.setMobileNo(mobile);
		effectDto.setCustIdCard(dataMap.get("custIdCard"));
		effectDto.setInterfaceCode(interfaceCode);
		effectDto.setLoanId(loanId);
//		effectDto.setProductType(dataMap.get("productType"));
		effectDto.setProductType("");
		effectDto = checkEffectInfo(effectDto);
		// 实时查询
		if (QUERY_REALTIME.equals(effectDto.getEffectFlag())) {
			return queryDirect(dataMap, serialNum, mobile, loanId, channel, interfaceCode, taskId);
		}
		// 接口停用
		if (QUERY_STOP.equals(effectDto.getEffectFlag())) {
			apiResponse.setCode(RespStatus.DISABLE.getCode());
			apiResponse.setMsg(RespStatus.DISABLE.getMsg());
			return apiResponse;
		}
		// 需要重新查询，并且时效性中间表要记录结果数据
		if (QUERY_REALTIME_AND_IN_RECORDS.equals(effectDto.getEffectFlag())) {
			apiResponse = queryDirect(dataMap, serialNum, mobile, loanId, channel, interfaceCode, taskId);
			// 查询成功，中间表需要插入数据
			if (apiResponse.getCode() == RespStatus.SUCCESS.getCode()) {
				saveEdcDataCallOperationRecords(effectDto);
			}
			return apiResponse;
		}
		// 无需重新查询，取历史数据
		if (QUERY_HISTORY.equals(effectDto.getEffectFlag())) {
			apiResponse.setCode(RespStatus.SUCCESS.getCode());
			apiResponse.setMsg(RespStatus.SUCCESS.getMsg());
			return apiResponse;
		}
		return apiResponse;
	}

	private ApiResponse<?> queryDirect(Map<String, String> dataMap, String serialNum, String mobile, String loanId,
			String channel, String interfaceCode, String taskId) {
		// 查询用户报告
		if (ThirdMethodConstants.MX0001.equals(interfaceCode)) {
			return MoXie.obtainUserMobileReport(dataMap, mobile, serialNum, loanId, channel);
		}

		// 获取账号运营商全部数据的聚合接口
		if (ThirdMethodConstants.MX0002.equals(interfaceCode)) {
			return MoXie.obtainAllDataDetails(mobile, taskId, serialNum, loanId, channel);
		}

		// 查询用户报告v2
		if (ThirdMethodConstants.MX0003.equals(interfaceCode)) {
			return MoXie.obtainUserMobileReportV2(dataMap, mobile, serialNum, loanId, channel);
		}

		// 魔蝎查询用户手机号是否实名认证
		if (ThirdMethodConstants.MX0006.equals(interfaceCode)) {
			return MoXie.realNameAuthentication(dataMap);
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ApiResponse<NumberReport> obtainUserMobileReport(Map<String, String> hashMap, String mobile,
			String serialNum, String loanId, String channel) {
		String interfaceCode = ThirdMethodConstants.MX0001;
		return (ApiResponse<NumberReport>) mxCommon(hashMap, serialNum, mobile, loanId, channel, interfaceCode, new ApiResponse<NumberReport>(), "");
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ApiResponse<AllDataDetails> obtainAllDataDetails(String mobile, String taskId, String serialNum,
			String loanId, String channel) {
		String interfaceCode = ThirdMethodConstants.MX0002;
		return (ApiResponse<AllDataDetails>) mxCommon(new HashMap<String, String>(), serialNum, mobile, loanId, channel, interfaceCode,
				new ApiResponse<AllDataDetails>(), taskId);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ApiResponse<MXReport> obtainUserMobileReportV2(Map<String, String> hashMap, String mobile, String serialNum,
			String loanId, String channel) {
		String interfaceCode = ThirdMethodConstants.MX0003;
		return (ApiResponse<MXReport>) mxCommon(hashMap, serialNum, mobile, loanId, channel, interfaceCode,
				new ApiResponse<MXReport>(), "");
	}

	@SuppressWarnings("unchecked")
	@Override
	public ApiResponse<RealNameAuthentication> realNameAuthentication(Map<String, String> dataMap) {
		String interfaceCode = ThirdMethodConstants.MX0006;
		return (ApiResponse<RealNameAuthentication>) mxCommon(dataMap, "", "", "", "", interfaceCode, new ApiResponse<RealNameAuthentication>(), "");
	}

	@Override
	public String createBankTask(JSONObject json) {
		return MoXie.createBankTask(json);
	}

	@Override
	public String createCarrierTask(JSONObject json) {
		return MoXie.createCarrierTask(json);
	}

	@Override
	public String verifySMSCode(String taskId, String code) {
		return MoXie.verifySMSCode(taskId, code);
	}

	@Override
	public MobileBasicV3 obtainUseMobileBasic(String taskId, String mobile) {
		return MoXie.obtainUseMobileBasic(taskId, mobile);
	}

	@Override
	public BillDetail obtainUserBills(String mobile, Map<String, String> map) {
		return MoXie.obtainUserBills(mobile, map);
	}

	@Override
	public VoiceCallDetailList obtainUserVoiceCalls(String mobile, Map<String, String> map) {
		return MoXie.obtainUserVoiceCalls(mobile, map);
	}

	@Override
	public ShortMessageDetailV2 obtainUserShortMessages(String mobile, Map<String, String> map) {
		return MoXie.obtainUserShortMessages(mobile, map);
	}

	@Override
	public String createChsiTask(JSONObject json) {
		return MoXie.createChsiTask(json);
	}

	@Override
	public StudentInfoList obtainStudentInfo(String taskId) {
		return MoXie.obtainStudentInfo(taskId);
	}

	@Override
	public MoxieResponse createFundTask(Map<String, String> map) {
		return MoXie.createFundTask(map);
	}

	@Override
	public ApiResponse<UserFundRecord> obtainUserFundRecord(String taskId, String serialNum, String loanId,
			String channel) {
		return MoXie.obtainUserFundRecord(taskId, serialNum, loanId, channel);
	}

	@Override
	public UserInfo obtainUserFundBaseRecord(String taskId) {
		return MoXie.obtainUserFundBaseRecord(taskId);
	}

	@Override
	public List<BillRecord> obtainUserFundExitRecord(String taskId) {
		return MoXie.obtainUserFundExitRecord(taskId);
	}

	@Override
	public List<LoanInfo> obtainUserFundBorrowRecord(String taskId) {
		return MoXie.obtainUserFundBorrowRecord(taskId);
	}

	@Override
	public List<LoanRepayRecord> obtainUserFundRepayRecord(String taskId) {
		return MoXie.obtainUserFundRepayRecord(taskId);
	}

	@Override
	public String createMailTask(JSONObject json) {
		return MoXie.createMailTask(json);
	}

	@Override
	public EmailBillList obtainCreditBillRecord(String taskId) {
		return MoXie.obtainCreditBillRecord(taskId);
	}

	@Override
	public EmailBillShoppingSheetList obtainCreditConsumBillRecord(String taskId) {
		return MoXie.obtainCreditConsumBillRecord(taskId);
	}

	@Override
	public EmailBillInstallmentList obtainCreditConsumPeriodBillRecord(String taskId) {
		return MoXie.obtainCreditConsumPeriodBillRecord(taskId);
	}

	@Override
	public String createInsurancePolicyTask(JSONObject json) {
		return MoXie.createInsurancePolicyTask(json);
	}

	@Override
	public InsurancePolicy obtainPolicyData(String taskId) {
		return MoXie.obtainPolicyData(taskId);
	}

	@Override
	public String getCityData() {
		return MoXie.getCityData();
	}

	@Override
	public MoxieResponse createCarrierTaskV2(Map<String, String> map) {
		return MoXie.createCarrierTaskV2(map);
	}

	@Override
	public MoxieResponse verifySMSCodeV2(Map<String, String> map) {
		return MoXie.verifySMSCodeV2(map);
	}

	@Override
	public MoxieTaskInfo searchCarrierStatus(String loanId) {
		return MoXie.searchCarrierStatus(loanId);
	}

	@Override
	public boolean taskIdStatusIsFail(String taskId) {
		return MoXie.taskIdStatusIsFail(taskId);
	}

	@Override
	public MoxieResponse createCarrierTaskForMall(Map<String, String> map) {
		return MoXie.createCarrierTaskForMall(map);
	}

	@Override
	public MoxieResponse createMoxieTask(Map<String, String> map) {
		return MoXie.createMoxieTask(map);
	}
	
	@Override
	public String getCarrierTaskStatus(String loanId) {
		return MoXie.getCarrierTaskStatus(loanId);
	}

	@Override
	public MoxieResponse verifySMSCodeForMall(Map<String, String> map) {
		return MoXie.verifySMSCodeForMall(map);
	}

	@Override
	public Map<String, Object> queryMxDataInfo(Map<String, String> map) {
		return MoXie.queryMxDataInfo(map);
	}

	@Override
	public Map<String, Object> queryMxDataInfo(Map<String, String> map, String productType) {
		return MoXie.queryMxDataInfo(map, productType);
	}

	@Override
	public ApiResponse<MXAlipayReport> alipayReportV6(String mobile, String taskId, String serialNum,
			String loanId, String channel) {
		return MoXie.alipayReportV6(mobile, taskId, serialNum, loanId, channel);
	}

	@Override
	public ApiResponse<MXAlipayZmsReport> alipayZmsReportV2(String mobile, String taskId, String serialNum, String loanId,
			String channel) {
		return MoXie.alipayZmsReportV2(mobile, taskId, serialNum, loanId, channel);
	}

	@Override
	public MoXieTaskResult runMoXieAlipayTask(String taskId, String loanId) {
		return MoXie.runMoXieAlipayTask(taskId, loanId);
	}

	@Override
	public MoXieTaskResult runMoXieCarrierTask(String taskId, String loanId) {
		return MoXie.runMoXieCarrierTask(taskId, loanId);
	}

	@Override
	public MXUserReportCall getReportMessage(String loanId, MoXieTaskType moXieTaskType) {
		return MoXie.getReportMessage(loanId, moXieTaskType);
	}

	@Override
	public List<MoxieTaskInfo> selectMoxieTaskInfo(String type) {
		return MoXie.selectMoxieTaskInfo(type);
	}

	@Override
	public int updateMoxieProcessing() {
		return MoXie.updateMoxieProcessing();
	}

}
